package com.mall.service.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.SmsHomeAdvertise;
import com.mall.model.vo.PageResult;
import com.mall.service.marketing.mapper.SmsHomeAdvertiseMapper;
import com.mall.service.marketing.service.SmsHomeAdvertiseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class SmsHomeAdvertiseServiceImpl extends ServiceImpl<SmsHomeAdvertiseMapper, SmsHomeAdvertise> implements SmsHomeAdvertiseService {

    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    //分页
    @Override
    public PageResult findList(String name, Integer type, String endTime, Integer pageNum, Integer pageSize) {
        Page<SmsHomeAdvertise> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(endTime)){
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format.parse(endTime);
                queryWrapper.lt(SmsHomeAdvertise::getEndTime,date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like(SmsHomeAdvertise::getName,name);
        }

        if (type != null){
            queryWrapper.eq(SmsHomeAdvertise::getType,type);
        }

        Page<SmsHomeAdvertise> advertisePage = smsHomeAdvertiseMapper.selectPage(page, queryWrapper);

        return new PageResult(pageNum,pageSize, (int) advertisePage.getTotal(),advertisePage.getRecords());

    }

    //修改
    @Override
    public void updateAdvertise(Integer id, SmsHomeAdvertise advertise) {
        if (advertise.getId() == null){
            advertise.setId(Long.valueOf(id));
        }
        smsHomeAdvertiseMapper.updateById(advertise);

    }
}
